package com.liu.resume.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 类、方法注释 懒加载片段
 *
 * @author creat  by:yts 2016/8/8
 * @author update by:
 */
public abstract class LazyFragment extends BaseFragment {
    protected boolean isVisible;
    /**
     * 数据是否已加载完毕
     */
    private boolean isLoadDataCompleted;
    private boolean isViewCreated;
    protected View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(getLayout(), container, false);
            initViews(view);
            isViewCreated = true;
        }
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isViewCreated && !isLoadDataCompleted) {
            isLoadDataCompleted = true;
            lazyLoad();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (getUserVisibleHint()) {
            isLoadDataCompleted = true;
            lazyLoad();
        }
    }

    protected abstract int getLayout();

    public abstract void initViews(View view);

    protected abstract void onInvisible();//不可见时需要处理的事件

    protected abstract void lazyLoad();//懒加载

    protected void onVisible() {
        lazyLoad();
    }//视图可见时需要处理的事件
}
